function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); // Not IE
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); // IE
	} else {
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}

function getStateList(countryId, baseUrl, targetSelectBoxId) {	
	var receiveReq = getXmlHttpRequestObject();
	var requestUrl = baseUrl + "/getStates.ems?countryId="+countryId;
	
	receiveReq.open("GET", requestUrl, true);
	receiveReq.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
	
	receiveReq.onreadystatechange = function() {
		if (receiveReq.readyState == 4) {			
			var stateList = eval(receiveReq.responseText);
			
			var stateSelectBox = document.getElementById(targetSelectBoxId);
			stateSelectBox.options.length = 0;
			
			var option = document.createElement("option");
		    option.text = "Please select state";
		    option.value = "0";
		    addOptions(stateSelectBox,option);
		    
			for(i=0; i < stateList.length; i++) {
				var option = document.createElement("option");
			    option.text = stateList[i].stateName + "[ "+ stateList[i].stateCode + " ] ";
			    option.value = stateList[i].stateId;
			    addOptions(stateSelectBox,option);					
			}				
		}			 
	};
	receiveReq.send(null);	
}

function addOptions(selectBox,option)
{
	try {
		selectBox.add(option, null); // Standard
    }catch(error) {
    	selectBox.add(option); // IE only
    }	
}

function getStaffList(roleId, baseUrl, targetSelectBoxId) {	
	var receiveReq = getXmlHttpRequestObject();
	var requestUrl = baseUrl + "/getAjaxStaffList.ems?roleId="+roleId;
	
	receiveReq.open("GET", requestUrl, true);
	receiveReq.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
	
	receiveReq.onreadystatechange = function() {
		if (receiveReq.readyState == 4) {
			alert(receiveReq.responseText);
			var staffList = eval(receiveReq.responseText);
			
			var stateSelectBox = document.getElementById(targetSelectBoxId);
			stateSelectBox.options.length = 0;
			for(i=0; i < staffList.length; i++) {
				var option = document.createElement("option");
			    option.text = staffList[i].firstName + " "+ staffList[i].lastName + "[" + staffList[i].userName + "]";
			    option.value = staffList[i].staffId;
			    addOptions(stateSelectBox,option);
			}				
		}			 
	};
	receiveReq.send(null);	
}

function getSessionList(schoolId, baseUrl, targetSelectBoxId) {	
	var receiveReq = getXmlHttpRequestObject();
	var requestUrl = baseUrl + "/getAjaxSessionList.ems?schoolId="+schoolId;
	
	receiveReq.open("GET", requestUrl, true);
	receiveReq.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
	
	receiveReq.onreadystatechange = function() {
		if (receiveReq.readyState == 4) {
			var sessionList = eval(receiveReq.responseText);
			
			var sessionSelectBox = document.getElementById(targetSelectBoxId);
			sessionSelectBox.options.length = 0;
			for(i=0; i < sessionList.length; i++) {
				var option = document.createElement("option");
			    option.text = sessionList[i].firstName + " "+ sessionList[i].lastName + "[" + sessionList[i].userName + "]";
			    option.value = sessionList[i].staffId;
			    addOptions(sessionSelectBox,option);
			}				
		}			 
	};
	receiveReq.send(null);	
}

function getSectionList(classId, baseUrl, targetSelectBoxId) {	
	var receiveReq = getXmlHttpRequestObject();
	var requestUrl = baseUrl + "/getAjaxSessionList.ems?classId="+classId;
	
	receiveReq.open("GET", requestUrl, true);
	receiveReq.setRequestHeader("Accept", "application/json, text/javascript, */*; q=0.01");
	
	receiveReq.onreadystatechange = function() {
		if (receiveReq.readyState == 4) {
			var sectionList = eval(receiveReq.responseText);
			
			var sectionSelectBox = document.getElementById(targetSelectBoxId);
			sectionSelectBox.options.length = 0;
			for(i=0; i < sectionList.length; i++) {
				var option = document.createElement("option");
			    option.text = sectionList[i].sessionName;
			    option.value = sectionList[i].sectionId;
			    addOptions(sectionSelectBox,option);
			}				
		}			 
	};
	receiveReq.send(null);	
}